/**
 * 
 */
package com.hkfn.rfb.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.bean.BaseModel;
import com.hkfn.rfb.rfb.bean.RfbParamModel;

import java.util.ArrayList;
import java.util.List;

public class TagListView extends FlowLayout implements OnClickListener, CompoundButton.OnCheckedChangeListener {

    private OnTagClickListener mOnTagClickListener;
    private OnCheckChangeListener onCheckChangeListener;
    private int mTagViewBackgroundResId;
    private int mTagViewTextColorResId;
    private List<BaseModel> models = new ArrayList<>();

    //当前点击项key
    private int currentLocation = 0;

    public int getCurrentLocation() {
        return currentLocation;
    }

    public void setCurrentLocation(int currentLocation) {
        this.currentLocation = currentLocation;
    }

    /**
     * @param context
     */
    public TagListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    /**
     * @param context
     * @param attributeSet
     */
    public TagListView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        // TODO Auto-generated constructor stub
        init();
    }

    /**
     * @param context
     * @param attributeSet
     * @param defStyle
     */
    public TagListView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        // TODO Auto-generated constructor stub
        init();
    }

    @Override
    public void onClick(View v) {
        if ((v instanceof LinearLayout)) {
            BaseModel localTag = (BaseModel) v.getTag();
            if (this.mOnTagClickListener != null) {
                this.mOnTagClickListener.onTagClick((LinearLayout) v, localTag, (int) v.getTag(R.id.tag_first));
            }
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if ((buttonView instanceof CheckBox)) {
            BaseModel localTag = (BaseModel) buttonView.getTag();
            if (this.onCheckChangeListener != null) {
                this.onCheckChangeListener.onCheckChange(isChecked, (int) buttonView.getTag(R.id.tag_first));
            }
        }
    }


    private void init() {

    }

    private void inflateTagView(BaseModel model, int location) {
        LinearLayout localTagView;

        RfbParamModel t = (RfbParamModel)model;
        localTagView = (LinearLayout) View.inflate(getContext(),
                R.layout.item_data_monitor_select_item, null);
        TextView tv_item_name = localTagView.findViewById(R.id.tv_item_name);
        tv_item_name.setText(t.getParamNameAndUnit());
        localTagView.setTag(t);
        localTagView.setTag(R.id.tag_first, location);

        CheckBox cb_item_select = localTagView.findViewById(R.id.cb_item_select);
        cb_item_select.setChecked(t.isSelected());
        cb_item_select.setTag(t);
        cb_item_select.setTag(R.id.tag_first, location);
        cb_item_select.setOnCheckedChangeListener(this);

        localTagView.setOnClickListener(this);

        addView(localTagView);
    }

    public void addTag(BaseModel modle, int location) {
        models.add(modle);
        inflateTagView(modle, location);
    }

    public List<BaseModel> getTags() {
        return models;
    }

    public View getViewByTag(BaseModel tag) {
        return findViewWithTag(tag);
    }

    public void removeTag(BaseModel tag) {
        models.remove(tag);
        removeView(getViewByTag(tag));
    }

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        mOnTagClickListener = onTagClickListener;
    }

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    public void setTagViewBackgroundRes(int res) {
        mTagViewBackgroundResId = res;
    }

    public void setTagViewTextColorRes(int res) {
        mTagViewTextColorResId = res;
    }

    public void setTags(List<? extends BaseModel> lists) {
        removeAllViews();
        models.clear();
        for (int i = 0; i < lists.size(); i++) {
            addTag(lists.get(i), i);
        }
    }

    public interface OnTagClickListener {
        void onTagClick(LinearLayout tagView, BaseModel tag, int location);
    }

    public interface OnCheckChangeListener {
        void onCheckChange(boolean isCheck, int location);
    }
}